import pickle
import pandas as pd
import numpy as np
f = open('models/NaiveBayes.model', 'rb')
s = f.read()
model = pickle.loads(s)

X_test = pd.read_csv("data/titanic_test.csv")
X_test["Sex_Cleaned"] = np.where(X_test["Sex"] == "male", 0, 1)
X_test["Embarked_Cleaned"] = np.where(X_test["Embarked"] == "S", 0,
                                    np.where(X_test["Embarked"] == "C", 1,
                                             np.where(X_test["Embarked"] == "Q", 2, 3
                                             )))

X_test = X_test[[
    "Pclass",
    "Sex_Cleaned",
    "Age",
    "SibSp",
    "Parch",
    "Fare",
    "Embarked_Cleaned"
]].dropna(axis=0, how="any")

features = [
    "Pclass",
    "Sex_Cleaned",
    "Age",
    "SibSp",
    "Parch",
    "Fare",
    "Embarked_Cleaned"
]

X_test = X_test[features].values



Y_test = pd.read_csv("data/gender_submission.csv")
Y_test = Y_test["Survived"].dropna(axis=0, how="any").values

print(X_test)
print(Y_test)

res = model.predict(X_test)
loss = 0
for i in range(0, len(res)):
    if res[i] != Y_test[i]:
        loss = loss + 1

print("模型的准确率为：", 1 - loss/len(res))

